//
//  HNSH_BusinessCircleViewController.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/2/27.
//

#import "HNSH_BusinessCircleViewController.h"

//view
#import "HNSH_BusinessCircleCell.h"

//vc
#import "HNSH_BusinessCircleDetailViewController.h"

@interface HNSH_BusinessCircleViewController ()

/* listAry */
@property (nonatomic, strong) NSMutableArray *listAry;

/* type */
@property (nonatomic, assign) NSInteger type;

@end

static NSString *HNSH_BusinessCircleCellID = @"HNSH_BusinessCircleCellID";

@implementation HNSH_BusinessCircleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //默认拿全国数据
    self.type = [[NSUserDefaults standardUserDefaults] integerForKey:@"KGetAllCityData"];
    
    [self configTableView];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tableViewReloadData) name:@"KBusinessCircleReload" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tableViewSelectCityReloadData) name:@"KBusinessCircleSelectCityReload" object:nil];
}

- (void)tableViewReloadData {
    [self headerRefreshing];
}

- (void)tableViewSelectCityReloadData {
    self.type = 1;
    //修改城市取指定城市数据
    [[NSUserDefaults standardUserDefaults] setInteger:1 forKey:@"KGetAllCityData"];
    [self headerRefreshing];
}

- (void)configTableView {
    [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, kTabBarHeight, 0));
    }];
    self.tableView.rowHeight = 132;
    [self.tableView registerClass:[HNSH_BusinessCircleCell class] forCellReuseIdentifier:HNSH_BusinessCircleCellID];
    
    [self setupEmpty:self.tableView image:[UIImage imageNamed:@"empty_list_data"] title:@""];
    [self setupRefresh:self.tableView option:ST_RefreshDefault];
}

- (void)refreshData:(NSInteger)page {
    NSDictionary *dict;
    NSString *codeString = @"";
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"KCurrentLocation"]) {
        dict = [[NSUserDefaults standardUserDefaults] objectForKey:@"KCurrentLocation"];
    }
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"KSelectLocationCode"]) {
        codeString = [[NSUserDefaults standardUserDefaults] objectForKey:@"KSelectLocationCode"];
    }
    [HNSH_ShopNetServer requestBusinessCircleShopListWithCategory_id:self.parameter
                                                             keyword:@"" type:self.type
                                                         region_code:[codeString isNotBlank] ? codeString : dict[@"region_code"]
                                                            latitude:[NSString stringWithFormat:@"%f",[dict[@"latitude"] doubleValue]]
                                                           longitude:[NSString stringWithFormat:@"%f",[dict[@"longitude"] doubleValue]]
                                                            page_num:page
                                                             success:^(ST_BaseNetModel * _Nullable responseObject) {
        [self endRefresh:YES];
        if (responseObject.code == 0) {
            if (page == 1) {
                [self.listAry removeAllObjects];
            }
            NSArray *array = [NSArray yy_modelArrayWithClass:[HNSH_BusinessCircleListModel class] json:responseObject.resultset[@"list"]];
            [self.listAry addObjectsFromArray:array];
            [self.tableView reloadData];
            [self endRefresh:array.count>=10];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

#pragma mark lazy
- (NSMutableArray *)listAry {
    if (!_listAry) {
        _listAry = [NSMutableArray arrayWithCapacity:0];
    }
    return _listAry;
}

#pragma mark - delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.listAry.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 8.01f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [UIView new];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HNSH_BusinessCircleCell *cell = [tableView dequeueReusableCellWithIdentifier:HNSH_BusinessCircleCellID];
    cell.model = self.listAry[indexPath.section];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HNSH_BusinessCircleDetailViewController *vc = [[HNSH_BusinessCircleDetailViewController alloc]init];
    vc.model = self.listAry[indexPath.section];
    [[UIViewControllerHelper findCurrentShowingViewController].navigationController pushViewController:vc animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
